﻿//-----------------------------------------------------------------------------------------//
//-     Writen by Raymond Honderdors
//-     2013-05-14
//-
//-     Code can be used as is, no warrenty
//-     https://solutionmanagementki.codeplex.com/
//-----------------------------------------------------------------------------------------//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Windows.Forms;
using System.IO;
using SMK_Explorer_2013.Forms.DockHelpers;
using WinFormsUI.Docking;
namespace SMK_Explorer_2013
{
    /// <summary>
    /// 
    /// </summary>
    public partial class MainForm : Form
    {
        /// <summary>
        /// 
        /// </summary>
        private Forms.DockHelpers.Explorer_Helper treeview1;
        /// <summary>
        /// 
        /// </summary>
        private LogonForm frmLogon = null;
        /// <summary>
        /// 
        /// </summary>
        private ToolStripStatusLabel tssl_treeviewpath = null;

        /// <summary>
        /// 
        /// </summary>
        private bool m_bSaveLayout = true;
        /// <summary>
        /// 
        /// </summary>
        private DeserializeDockContent m_deserializeDockContent;
        private void loadTreeView()
        {
            // 
            // treeview1
            // 
            this.treeview1 = new Explorer_Helper();
            this.treeview1.ClientSize = new System.Drawing.Size(275, 518);
            this.treeview1.Location = new System.Drawing.Point(-3, -190);
            this.treeview1.Name = "treeview1";
            this.treeview1.Text = "Explorer_Helper";
            this.treeview1.Visible = false;
            this.treeview1.tvs += new SMK_Explorer_2013.Forms.DockHelpers.Explorer_Helper.TreeviewSelected(this.treeview1_tvs);
            this.treeview1.tvserveradded += new SMK_Explorer_2013.Forms.DockHelpers.Explorer_Helper.ServerAdded(this.treeview1_tvserveradded);
            
            this.treeview1.RightToLeft = RightToLeft;
            this.treeview1.Dock = DockStyle.Left;
            this.treeview1.Show(this.dockPanel1);
        }
        /// <summary>
        /// 
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            showRightToLeft.Checked = (RightToLeft == RightToLeft.Yes);
            RightToLeftLayout = showRightToLeft.Checked;

            loadTreeView();
            m_deserializeDockContent = new DeserializeDockContent(GetContentFromPersistString);
            
            if (Environment.Is64BitProcess)
            {
                this.Text = "SMK Explorer v" + this.ProductVersion.ToString() + " (64)"; 
            }
            else
            {
                this.Text = "SMK Explorer v" + this.ProductVersion.ToString();
            }
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exitWithoutSavingLayout_Click(object sender, EventArgs e)
        {
            m_bSaveLayout = false;
            Close();
            m_bSaveLayout = true;
        }

        private IDockContent GetContentFromPersistString(string persistString)
        {
            if (persistString == typeof(Explorer_Helper).ToString())
            {
                return treeview1;
            }
            else
            {
                return null;
            }
            //else if (persistString == typeof(DummyPropertyWindow).ToString())
            //    return m_propertyWindow;
            //else if (persistString == typeof(DummyToolbox).ToString())
            //    return m_toolbox;
            //else if (persistString == typeof(DummyOutputWindow).ToString())
            //    return m_outputWindow;
            //else if (persistString == typeof(DummyTaskList).ToString())
            //    return m_taskList;
            //else
            //{
            //    // DummyDoc overrides GetPersistString to add extra information into persistString.
            //    // Any DockContent may override this value to add any needed information for deserialization.

            //    string[] parsedStrings = persistString.Split(new char[] { ',' });
            //    if (parsedStrings.Length != 3)
            //        return null;

            //    if (parsedStrings[0] != typeof(DummyDoc).ToString())
            //        return null;

            //    DummyDoc dummyDoc = new DummyDoc();
            //    if (parsedStrings[1] != string.Empty)
            //        dummyDoc.FileName = parsedStrings[1];
            //    if (parsedStrings[2] != string.Empty)
            //        dummyDoc.Text = parsedStrings[2];

            //    return dummyDoc;
            //}
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void showRightToLeft_Click(object sender, EventArgs e)
        {
           // CloseAllContents();
            if (showRightToLeft.Checked)
            {
                this.RightToLeft = RightToLeft.No;
                this.RightToLeftLayout = false;
            }
            else
            {
                this.RightToLeft = RightToLeft.Yes;
                this.RightToLeftLayout = true;
            }
            treeview1.RightToLeftLayout = this.RightToLeftLayout;
            showRightToLeft.Checked = !showRightToLeft.Checked;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ServerType"></param>
        /// <param name="ServerName"></param>
        public void frmload_onConnect(string ServerType, string ServerName)
        {
            frmLogon.Close();
            frmLogon.Dispose();

            //this.MainFormStatusBar.Items.Add(new ToolStripStatusLabel(ServerName));
            this.treeview1.Addserver(ServerName,ServerType);
        }
        /// <summary>
        /// 
        /// </summary>
        public void frmload_onCancel()
        {
            frmLogon.Close();
            frmLogon.Dispose();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void connectObjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Open Form for logon
            frmLogon = new LogonForm();
            //frmLogon.Activate();
            
            this.frmLogon.eOnCancel += new dOnCancel(this.frmload_onCancel);
            this.frmLogon.eonConnect += new dOnConnect(this.frmload_onConnect);
            frmLogon.ShowDialog(this);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dISCONNECTOBJECTToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="key"></param>
        private void treeview1_tvs(object sender, SMK_Treeview.Treeview_Properties key)
        {
            this.SuspendLayout();
            //this.MainFormTabControl.TabPages.Clear();
            if (tssl_treeviewpath == null)
            {
                tssl_treeviewpath = new ToolStripStatusLabel(key.key);
                this.MainFormStatusBar.Items.Add(tssl_treeviewpath);
            }
            else
            {
                tssl_treeviewpath.Text = key.key;
            }

            //this.MainFormTabControl.ImageList = key.winimagelist;
            TabPage tp = new TabPage(key.key);
            tp.ImageIndex = key.winimagelist.Images.IndexOfKey(key.imagekey);
            //tp.ImageKey = key.imagekey;
            
            key.winlist.Dock = DockStyle.Fill;
            key.winlist.View = View.Details;
            key.winlist.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            tp.Controls.Add(key.winlist);
            //this.MainFormTabControl.TabPages.Add(tp);
            this.ResumeLayout();
            this.treeview1.Focus();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="servername"></param>
        private void treeview1_tvserveradded(string servername)
        {
            this.tsbtndisconnectObject.Enabled = true;
        }
    }
}
